import {
  request,
  requestHasToken
} from '../../service/network.js'
import {
  previewImg,
  previewImgList
} from '../../behavior/previewImg/previewImg.js'
Page({
  behaviors: [previewImg, previewImgList], //  继承behavior.js里面的properties，data，methods
  /**
   * 页面的初始数据
   */
  data: {
    baseurl: App.config.baseApi,
    isSubmit: false,
    warn: "",
    pic: '',
    genderIndex: 0,
    genders: ['男', '女'],
    marriagesIndex: 0,
    marriages: ['未婚', '离异'],
    typeIndex: 0,
    types: ['公开', '隐藏'],
    addressIndex: 0,
    addresses: ['鄞州区', '海曙区', '江北区', '江东区', '北仑区', '镇海区', '奉化区', '象山', '慈溪', '余姚', '宁海', '台州', '其他'],
    info: {
      id: '',
      userAvatar: '',
      userNickName: '',
      openid: '',
      userId: '',
      nickname: '',
      gender: '1',
      age: '',
      phone: '',
      marriage: '未婚',
      education: '',
      occupation: '',
      car: '',
      house: '',
      pic: '',
      height: '',
      weight: '',
      comment: '',
      nativeplace: '',
      address: '鄞州区',
      hobby: '',
      requirement: '',
      type: '公开',
      state: ''
    }
  },
  onLoad: function (options) {
    

  },
  onShareAppMessage: function () {

  },
  handleGender(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      genderIndex: e.detail.value,
      'info.gender': e.detail.value + 1
    })
  },
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ageIndex: e.detail.value,
      'info.age': e.detail.value
    })
  },
  handleMarriage: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      marriagesIndex: e.detail.value,
      'info.marriage': this.data.marriages[e.detail.value]
    })
  },
  handleType: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      typeIndex: e.detail.value,
      'info.type': this.data.types[e.detail.value]
    })
  },
  handleAddresses: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      addressIndex: e.detail.value,
      'info.address': this.data.addresses[e.detail.value]
    })
  },
  handlePic(res) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        this.setData({
          'pic': tempFilePaths[0],
        })
        wx.uploadFile({
          url: App.config.baseApi + 'api/xcxFile/upload',
          header: {
            Authorization: 'Bearer ' + wx.getStorageSync('token'),
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8' // 默认值
          },
          filePath: this.data.pic,
          name: 'file',
          formData: {},
          success: (res2) => {
            console.log(res2);

            let infoic = JSON.parse(res2.data).url
            this.setData({
              'info.pic': infoic,
            })
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    wx.showToast({
      title: '保存成功!',
      icon: 'success',
      duration: 1500,
      complete:() => {

      }
    })
    setTimeout(() => {
      
      wx.navigateTo({
        url: '/pages/infomation/infomation'
      })
    }, 1000);
  },
})